﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using SmartProject.BusinessObjects;

namespace SmartProject.BusinessRules
{
    public class WorkStatusToInProgress : BusinessRule
    {
        public override string Name
        {
            get { return "Work Status In Progress Rule"; }
        }

        public override string Description
        {
            get { return "Work must be assigned and in a sprint cycle before be able to be in progress."; }
        }

        public override void Implement(Entity data)
        {
            Data.DataParameter dataParameter = data.GetDataParameter("StatusId", false);
            if (dataParameter.Value != null)
            {
                Status status = new Status();
                string statusString = status.GetValue((int)dataParameter.Value);
                // We are specifically looking for In Progress status to do validation.
                if (statusString == "In Progress")
                {
                    int workId = -1;
                    // Find WorkId.
                    foreach (Data.DataParameter d in data.DataParameters)
                    {
                        if (d.GetName(false) == "WorkId")
                        {
                            workId = Convert.ToInt32(d.Value);
                        }
                    }

                    if (workId > -1)
                    {
                        // Find current status.
                        Work work = new Work();
                        work.SetAttribute("WorkId", workId);
                        work.Load();

                        int currentStatusId = (int)work.GetAttributeValue("StatusId");
                        string currentStatus = status.GetValue((int)dataParameter.Value);
                        if (currentStatus != "In Progress" && currentStatus != "Assigned")
                        {
                            throw new Exceptions.BusinessRuleViolation(this.Description);
                            /*
                            base.EvaluationMessage = this.Description;
                            Logging.Logger.Log("WorkStatusToInProgress", "Evaluate",
                                base.EvaluationMessage, "Error");
                            return false;
                            */
                        }
                    }
                }
            }
        }

        public override bool QueryBooleanProperty(string propertyName)
        {
            // Do nothing.
            return true;
        }
    }
}
